# -*- coding: utf-8 -*-

import os
import wx
import wx.adv
import wx.richtext
#import suffix
#import wx.dataview
#import wx.lib.filebrowsebutton
import time
import shutil
import images

import tree
import boxlist
import configuration
import config
import public
#import demo
import mybook




class MyFrame1 ( wx.Frame ):
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = "项目笔记", pos = wx.DefaultPosition, size = wx.Size( 800,520 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
        self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        bSizer2 = wx.BoxSizer( wx.HORIZONTAL )

        bSizer3 = wx.BoxSizer( wx.HORIZONTAL )

        bSizer4 = wx.BoxSizer( wx.VERTICAL )
        self.panel_Left   = wx.Panel(self)
        self.panel_center = wx.Panel(self)
        self.panel_right  = wx.Panel(self)


        # left
        nm = wx.StaticBox(self.panel_Left, -1, '')
        bSizer_v = wx.StaticBoxSizer(nm, wx.VERTICAL)
        
        self.m_searchCtrl2 = wx.SearchCtrl(self.panel_Left, style=wx.TE_PROCESS_ENTER)
        self.m_listctrl1 = boxlist.mListCtrl(self.panel_Left)
        bSizer_v.Add(self.m_searchCtrl2, 0, wx.FIXED_MINSIZE|wx.EXPAND, 10)
        bSizer_v.Add(self.m_listctrl1, 1, wx.EXPAND, 10)
        self.panel_Left.SetSizer(bSizer_v)

        self.m_searchCtrl2.ShowSearchButton( True )
        self.m_searchCtrl2.ShowCancelButton( True )
        searmenu = wx.Menu()
        self.Bind(wx.EVT_MENU, self.on_menu, searmenu.Append(-1, "笔记名", kind=wx.ITEM_RADIO))
        self.Bind(wx.EVT_MENU, self.on_menu, searmenu.Append(-1, "笔记id", kind=wx.ITEM_RADIO))
        self.m_searchCtrl2.SetMenu( searmenu )
        

        # center
        bSizer_v = wx.BoxSizer( wx.VERTICAL )
        #nm = wx.StaticBox(self.panel_center, -1, '')
        #bSizer_v_1 = wx.StaticBoxSizer(nm, wx.HORIZONTAL)
        self.book = mybook.Mbook(self.panel_center)
        self.setting = configuration.mconfig(self.panel_center)
        bSizer_v.Add(self.book, 1, wx.EXPAND, 10)
        bSizer_v.Add(self.setting, 1, wx.ALL|wx.EXPAND, 10)
        #bSizer_v.Add( bSizer_v_1, 1, wx.ALL|wx.EXPAND, 5 )
        self.panel_center.SetSizer(bSizer_v)

        self.book.FileDrop = public.FileDrop
 
        
        # right
        nm = wx.StaticBox(self.panel_right, -1, '')
        bSizer_v = wx.BoxSizer(wx.VERTICAL)
        bSizer_v_1 = wx.StaticBoxSizer(nm, wx.VERTICAL)

        self.textbok = wx.StaticText(self.panel_right, label="笔记一", style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.textbok.SetFont(wx.Font(10, wx.FONTFAMILY_SCRIPT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_EXTRALIGHT))
        self.mtree = tree.mTreeCtrl(self.panel_right, -1, style=wx.TR_HAS_BUTTONS)
        bSizer_v_1.Add( self.textbok, 1, wx.EXPAND, 5 )
        bSizer_v.Add( bSizer_v_1, 0, wx.ALL|wx.EXPAND, 5 )
        bSizer_v.Add( self.mtree, 1, wx.ALL|wx.EXPAND, 5 )
        self.panel_right.SetSizer(bSizer_v)

        #init
        #self.panel_Left.Show(False)
        self.setting.Show(False)
        bSizer = wx.BoxSizer( wx.HORIZONTAL )
        bSizer.Add( self.panel_Left, 0, wx.EXPAND, 5 )
        bSizer.Add( self.panel_center, 4, wx.EXPAND, 5 )
        bSizer.Add( self.panel_right, 2, wx.EXPAND, 5 )
        self.panel_right.Show(False)
        self.panel_Left.Show(False)
        self.SetSizer( bSizer )
        self.Layout()

        self.Centre( wx.BOTH )
        
        self.MakeMenuBar({"笔记": mybook.fileMenu(self), "标签": mybook.labelMenu(self)})
        self.SetDropTarget(public.FileDrop(self.book))
        self.OnNewLabel(None)


    def MakeMenuBar(self, call):
        mb = wx.MenuBar()
        for k, v in call.items():
            mb.Append(v, k)
        self.SetMenuBar(mb)

    def OnNewLabel_python(self, eve):
        self.book.mAddPage(mybook.Book_stc(self.book, None, mybook.TEXT_TYPE_PY),  "新标签" + str(self.book.count))

    def OnNewLabel_lua(self, eve):
        self.book.mAddPage(mybook.Book_stc(self.book, None, mybook.TEXT_TYPE_LUA),  "新标签" + str(self.book.count))

    def OnNewLabel_table(self, eve):
        self.book.mAddPage(mybook.Book_table(self.book, None, mybook.TEXT_TYPE_XLSX),  "新标签" + str(self.book.count))

    def OnNewLabel_shell(self, eve):
        self.book.mAddPage(mybook.Book_shell(self.book, None, mybook.TEXT_TYPE_PY),  "新标签" + str(self.book.count))

    def OnNewLabel_els(self, eve):
        self.book.mAddPage(mybook.Tetris(self.book),  "新标签" + str(self.book.count))

    def OnNewLabel(self, eve):
        self.book.mAddPage(mybook.Book_text(self.book, None, None, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER),  "新标签" + str(self.book.count))

    def OnCloseLabel(self, eve):
        # 关闭标签
        if self.book.count > 0:
            answer = wx.MessageBox("笔记还没有保存，确定关闭标签吗？", "提示", wx.YES_NO, self)
            if answer == wx.YES:
                self.book.DeletePage(self.book.GetSelection())
                self.book.count -= 1
        if not self.book.count:
            self.OnNewLabel(eve)

    def OnDelLable(self, eve):
        # 删除标签
        _id = self.book.GetSelection()
        if _id != -1:
            self.book.DeletePage(_id)
            self.book.count -= 1
        if not self.book.count:
            self.OnNewLabel(eve)

    def OnNewBook(self, eve):
        # 新建笔记
        dlg = public.myNewbook("笔记名", "笔记命名错误", self, -1, "新建笔记")
        val = dlg.ShowModal()
        print(val, dlg.ok.GetId())
        if val == dlg.ok.GetId():
            m_dir = public.Getid_path(config.M_PATH)
            title = dlg.text.GetValue()
            os.mkdir(m_dir)
            os.mkdir(os.path.join(m_dir, os.path.basename(m_dir)))
            print(self.m_listctrl1.GetItemCount()+1)
            item = public.Getid(self.m_listctrl1.GetItemCount(),title,_id)
            #item.SetId(1)
            #item.SetData(_id)
            _long = self.m_listctrl1.InsertItem(item)
            #print(_long)
        dlg.Destroy() 

    def OnOpenBook(self, eve):
        # 导入笔记
        dlg = wx.FileDialog(self, "导入笔记", config.CHDIR, "", "ZIP 文件 (*.zip)|*.zip|RAR 文件 (*.rar)|*.rar|7Z 文件 (*.7z)|*.7z", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        dlg.ShowModal()
        path = dlg.GetPath()
        dlg.Destroy()
        print(path)

    def OnSaveBook(self, eve):
        # 保存笔记
        pass

    def OnExitBook(self, eve):
        # 退出
        self.Close(True)
        wx.Exit()

    def OnSearchBook(self, eve, ised=True):
        # 搜索笔记
        if self.setting.IsShown():
            self.setting.Show(False)
        if self.panel_Left.IsShown():
            self.panel_Left.Show(False)
        else:
            if ised:
                self.panel_Left.Show(True)
                self.m_listctrl1._width()
                self.OnshuBook(eve, False)
        self.Layout()
        self.Refresh()

    def OnshuBook(self, eve, ised=True):
        # 笔记树
        if self.panel_right.IsShown():
            self.panel_right.Show(False)
        else:
            if ised:
                self.panel_right.Show(True)
                self.OnSearchBook(eve, False)
        self.Layout()
        self.Refresh()

    def OnExportBook(self, eve):
        # 导出笔记
        print(eve)

    def on_menu(self, eve):
        # 菜单
        print(eve.GetEventObject())
        print(eve.GetEventObject().GetLabelText(eve.Id))
        print(eve.GetEventObject().IsChecked(eve.Id))

        #print(self.m_searchCtrl2.IsChecked)
        #print(dir(eve), eve.Id)

if __name__ == '__main__':
    app = wx.App()
    MyFrame1(None).Show(True)
    app.MainLoop()
